package com.it.action;



import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.biz.StudentBiz;
import com.it.pojo.Student;
import com.jkoss.pojo.AjaxReturn;
import com.jkoss.tool.Page;

@Controller
public class StudentAction {
	@Resource
	private StudentBiz studentBiz;
	/*
	 * 获取学生列表
	 */
	@RequestMapping("getAllStudent.do")
	public String getAllStudent(Model model,Page<Student> page ) {
		if(page==null) {
			page = new Page<Student>();
		}
		page.setResults(studentBiz.findAll(page));
		model.addAttribute("page",page);
		return "students/listStudent.jsp";
	}

	/*
	 * 增加学生信息
	 */
	@RequestMapping("addStu.do")
	public String addStudent(Model model,Student student) {
		if(student!=null) {
			studentBiz.insert(student);
		}
		return getAllStudent(model,null);
		
	}
	/*
	 * 根据主键删除学生信息
	 */
	@RequestMapping("delStu.do")
	public String deleteByExample(Model model,Integer studentid) {
		studentBiz.deleteByPrimaryKey(studentid);
		return getAllStudent(model, null);
	}
	
	/*
	 * 根据主键更新学生信息
	 */
	@RequestMapping("updStu.do")
	public String updStu(Model model,Student student) {
		if(student!=null) {
			studentBiz.updateByPrimaryKey(student);
			return getAllStudent(model, null);
		}else
			System.out.println("fail");
		return getAllStudent(model, null);
		
	}

	/*
	 * ajax异步传输数据
	 */
	@RequestMapping("ajOneStu.do")
	@ResponseBody
	public AjaxReturn ajOneCou(Integer studentid) {
		AjaxReturn ajaxReturn = new AjaxReturn();
		Student student = studentBiz.selectByPrimaryKey(studentid);
		if(student!=null) {
			ajaxReturn.setHead("ok");
			ajaxReturn.setBody(student);
		}else {
			ajaxReturn.setHead("err");
		}
		return ajaxReturn;
	}
}
